package com.dingqb.myapplication.paint;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.dingqb.myapplication.R;

/**
 * Create by dingqb
 * On 2019-08-20
 * Description:
 */
public class PaintView extends View {
    private static final String TAG = "PaintView";

    public PaintView(Context context) {
        super(context);
    }

    public PaintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);

    }

    public PaintView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);


    }

    void initAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mystyle);
            int count = typedArray.getIndexCount();
            for (int i = 0; i < count; i++) {
                int attr = typedArray.getIndex(i);
                switch (attr) {
                    case R.styleable.mystyle_text_null:
                        boolean isNull = typedArray.getBoolean(attr, false);
                        Log.e(TAG, "PaintView: isNull==" + isNull);
                        break;
                    case R.styleable.mystyle_textcolor:
                        int color = typedArray.getColor(attr, 0);
                        Log.e(TAG, "PaintView: color==" + color);
                        break;
                    case R.styleable.mystyle_text_size:
                        int size = typedArray.getInteger(attr, 0);
                        Log.e(TAG, "initAttrs: size==" + size);
                        break;
                }
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // circle

//        drawCircle(canvas);
//        drawShader(canvas);
//        drawRecangle(canvas);
//        drawPath(canvas, 0);

//        canvas_animation(canvas);

        Paint pp=new Paint();
        pp.setColor(Color.RED);
        canvas.drawColor(Color.GRAY);
        canvas.save();
        canvas.clipRect(new Rect(100,100,400,400));
        canvas.drawColor(Color.BLUE);
        canvas.restore();
        canvas.drawColor(Color.WHITE);
        canvas.drawLine(0,110,400,200,pp);

    }

    private void canvas_animation(Canvas canvas) {
        Paint paint_green = generatePaint(Color.GREEN, Paint.Style.FILL, 5);
        Paint paint_red   = generatePaint(Color.RED, Paint.Style.STROKE, 5);

        Rect rect1 = new Rect(300,10,500,100);
        canvas.drawRect(rect1, paint_red); //画出原轮廓

        canvas.rotate(30);//顺时针旋转画布
        canvas.drawRect(rect1, paint_green);//画出旋转后的矩形
    }

    private Paint generatePaint(int color,Paint.Style style,int width)
    {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStyle(style);
        paint.setStrokeWidth(width);
        return paint;
    }


    private void drawPath(Canvas canvas, int i) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        Path path = new Path();
        paint.setStrokeWidth(5);
        path.addCircle(200 + i * 10, 200 + i * 10, 100, Path.Direction.CCW);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            path.addOval(200, 200, 400, 400, Path.Direction.CW);
//        }
        canvas.drawPath(path, paint);

        paint.setTextSize(28);
        paint.setColor(Color.GRAY);
        canvas.drawTextOnPath("大风起兮云飞扬，威加海内兮归故乡", path, 0, 28, paint);

    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };

    void drawRecangle(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        canvas.drawLine(0, 100, getMeasuredWidth(), 100, paint);
        canvas.drawLine(0, 200, getMeasuredWidth(), 200, paint);
        canvas.drawLine(100, 0, 100, getMeasuredHeight(), paint);
        canvas.drawLine(200, 0, 200, getMeasuredHeight(), paint);

        paint.setColor(Color.RED);
        Rect r = new Rect(100, 100, 200, 200);
        canvas.drawRect(r, paint);
        Rect r2 = new Rect(300, 100, 400, 200);
        canvas.drawRect(r2, paint);
        // 圆角矩形
        RectF r3 = new RectF(500, 100, 600, 200);
        canvas.drawRoundRect(r3, 10f, 10f, paint);
        paint.setColor(Color.GREEN);
        canvas.drawOval(r3, paint);

    }

    void drawShader(Canvas canvas) {
        Paint paint = new Paint();
        //对单独的View在运行时阶段禁用硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(2);
        paint.setAntiAlias(true);
        paint.setTextSize(50);
        paint.setShadowLayer(10, 25, 25, Color.GREEN);
        canvas.drawText("Shadow-", 100, 100, paint);
        canvas.drawCircle(200, 200, 50, paint);

    }

    void drawCircle(Canvas canvas) {
        // circle
        Paint paint = new Paint();
        paint.setColor(Color.RED);
//        paint.setStyle(Paint.Style.STROKE);
        // 设置渐变效果，
        paint.setShader(new LinearGradient(200, 200, 200, 300, new int[]{Color.GREEN, Color.YELLOW}, null, Shader.TileMode.CLAMP));
        //
        canvas.drawCircle(200, 200, 100, paint);
        //
    }
}
